package main

import (
	"fmt"

	"codingq92.com/thrift_demo/gen/thrift/interfaces"
	"codingq92.com/thrift_demo/services"
	"github.com/apache/thrift/lib/go/thrift"
)

func main() {
	// 1. 创建传输层（TCP 监听）
	addr := ":18000"
	transport, err := thrift.NewTServerSocket(addr)
	if err != nil {
		panic(fmt.Sprintf("创建监听失败: %v", err))
	}
	defer func(transport *thrift.TServerSocket) {
		_ = transport.Close()
	}(transport)
	// 2. 设置协议（二进制协议）
	protocolFactory := thrift.NewTBinaryProtocolFactoryConf(&thrift.TConfiguration{})
	// 3. 注册服务实现
	processor := interfaces.NewThriftDemoServiceProcessor(&services.ThriftServerImpl{})
	// 4. 创建并启动服务
	server := thrift.NewTSimpleServer4(
		processor,
		transport,
		thrift.NewTTransportFactory(),
		protocolFactory,
	)
	fmt.Printf("服务端启动，监听地址: %s\n", addr)
	if err := server.Serve(); err != nil {
		panic(fmt.Sprintf("服务启动失败: %v", err))
	}
}
